/*
 * DisclosureTriangle.java
 *
 * Created on November 15, 2006, 7:41 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.framework.gui;

import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

/**
 *
 * @author grant
 */
public class DisclosureTriangle extends FakeWidget
{
    /** Key for checking triangle state */
    public final static String EXPANDED_KEY = "expanded";
    
    /** Color to draw the triangle normally. */
    private final static AlphaColor NORMAL_COLOR = new AlphaColor(106, 106, 106);
    /** Color to draw the triangle when it's selected (being pressed; the mouse is down) */
    private final static AlphaColor PRESSED_COLOR = new AlphaColor(60, 60, 60);
    /** Size to make disclosure triangles. */
    private final static Point DEFAULT_SIZE = new Point(13, 13);
    /** List of points to draw at when the control is collapsed. */
    private final static int[] COLLAPSED_POLYGON = new int[] {2, 2, 2, 12, 11, 7};
    /** List of points to draw at when the control is expanded. */
    private final static int[] EXPANDED_POLYGON = new int[] {2, 2, 12, 2, 7, 11};
    /** List of points to draw at when the control is pressed. */
    private final static int[] PRESSED_POLYGON = new int[] {7, 0, 9, 9, 0, 7};
    
    
    /** True if the triangle is expanded (pointing down), false if it's not i(pointing right) */
    private boolean expanded;
    /** Track whether or not the control is being pressed. */
    private boolean pressed;
    
    /** Listener to track insided-ness. */
    private MouseListener mouseListener = new MouseListener()
    {
        public void mouseEntered(MouseEvent e)
        {
            pressed = true;
            redraw();
        }
        public void mouseExited(MouseEvent e)
        {
            pressed = false;
            redraw();
        }
    };
    
    /** Creates a new instance of DisclosureTriangle */
    public DisclosureTriangle()
    {
        bounds.width = DEFAULT_SIZE.x;
        bounds.height = DEFAULT_SIZE.y;
    }
    
    public boolean mouseDown(MouseEvent e)
    {
        pressed = true;
        addMouseListener(mouseListener);
        redraw();
        return true;
    }
    
    public boolean mouseUp(MouseEvent e)
    {
        removeMouseListener(mouseListener);
        if (pressed)
            toggle();
        pressed = false;
        return true;
    }
    
    /** Toggles the button's state. */
    public void toggle()
    {
        setExpanded(!expanded);
    }
    
    /**
     * Sets whether or not the triangle is expanded.
     * @param b True to expand the triangle (make it point down), false to close
     * the triangle (make it point right)
     * @see #getExpanded()
     */
    public void setExpanded(boolean b)
    {
        if (b != expanded)
        {
            expanded = b;
            didChangeValueForKey(EXPANDED_KEY);
            redraw();
        }
    }
    
    /**
     * Gets whether or not the triangle is expanded.
     * @return True if the triangle is expanded (pointing down), false if it
     * is closed (pointing to the right)
     * @see #setExpanded(boolean)
     */
    public boolean getExpanded()
    {
        return expanded;
    }

    /**
     * Paints the control.
     * @param g Graphics context to use to do painting.
     */
    public void paintComponent(GC g)
    {
        if (pressed)
        {
            PRESSED_COLOR.setBackground(g);
            g.fillPolygon(PRESSED_POLYGON);
        } else {
            NORMAL_COLOR.setBackground(g);
            if (expanded)
            {
                g.fillPolygon(EXPANDED_POLYGON);
            } else {
                g.fillPolygon(COLLAPSED_POLYGON);
            }
        }
    }
    
}
